package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.converter.IFieldTypeConverter;
import com.mazaiting.generator.domain.entity.FieldTypeEntity;
import com.mazaiting.generator.domain.form.FieldTypeForm;
import com.mazaiting.generator.domain.info.FieldTypeInfo;
import com.mazaiting.generator.domain.query.FieldTypeQuery;
import com.mazaiting.generator.domain.vo.FieldTypeVO;
import com.mazaiting.generator.mapper.IFieldTypeMapper;
import com.mazaiting.generator.service.IFieldTypeService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 字段类型管理
 *
 * @author 阿沐 babamu@126.com
 */
@Service
@RequiredArgsConstructor
public class FieldTypeServiceImpl extends ServiceImpl<IFieldTypeMapper, FieldTypeEntity> implements IFieldTypeService {

    private final IFieldTypeConverter fieldTypeConverter;

    @Override
    public BasePageVO<FieldTypeVO> page(FieldTypeQuery query) {
        // 分页查询
        Page<FieldTypeEntity> entityPage = lambdaQuery()
                // 字段类型
                .like(StringUtils.hasLength(query.getColumnType()), FieldTypeEntity::getColumnType, query.getColumnType())
                // 属性类型
                .like(StringUtils.hasLength(query.getAttrType()), FieldTypeEntity::getAttrType, query.getAttrType())
                .page(new Page<>(query.getPageNum(), query.getPageSize()));
        // entity -> vo
        Page<FieldTypeVO> voPage = fieldTypeConverter.entity2VO(entityPage);
        return BasePageVO.success(voPage);
    }

    @Override
    public Set<String> getListAll() {
        return lambdaQuery().select(FieldTypeEntity::getAttrType).list().stream().map(FieldTypeEntity::getAttrType).collect(Collectors.toSet());
    }

    @Override
    public Map<String, FieldTypeInfo> getMap() {
        return lambdaQuery().select(FieldTypeEntity::getColumnType, FieldTypeEntity::getAttrType, FieldTypeEntity::getPackageName).list()
                .stream().map(entity -> new FieldTypeInfo(entity.getColumnType(), entity.getAttrType(), entity.getPackageName()))
                .collect(Collectors.toMap(info -> info.getColumnType().toLowerCase(), Function.identity()));
    }

    @Override
    public boolean save(FieldTypeForm fieldTypeForm) {
        // form -> entity
        FieldTypeEntity entity = fieldTypeConverter.form2Entity(fieldTypeForm);
        return save(entity);
    }

    @Override
    public boolean update(FieldTypeForm fieldTypeForm) throws EmptyObjectException {
        // 根据 ID 查询实体信息
        FieldTypeEntity fieldTypeEntity = getById(fieldTypeForm.getId());
        if (Objects.isNull(fieldTypeEntity)) {
            throw new EmptyObjectException(ResultCode.COMMON_FAIL.getCode(), "字段类型信息不存在");
        }
        // 将表单信息注入到实体
        fieldTypeConverter.form2Entity(fieldTypeForm, fieldTypeEntity);
        // 更新
        return updateById(fieldTypeEntity);
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return removeByIds(idList);
    }

    @Override
    public FieldTypeVO getInfoById(Long id) {
        FieldTypeEntity fieldTypeEntity = getById(id);
        if (Objects.isNull(fieldTypeEntity)) {
            log.warn("实体信息不存在");
        }
        return fieldTypeConverter.entity2VO(fieldTypeEntity);
    }
//    @Override
//    public PageResult<FieldTypeEntity> page(Query query) {
//        IPage<FieldTypeEntity> page = baseMapper.selectPage(
//                getPage(query),
//                getWrapper(query)
//        );
//        return new PageResult<>(page.getRecords(), page.getTotal());
//    }
//
//    @Override
//    public Set<String> getPackageByTableId(Long tableId) {
//        Set<String> importList = baseMapper.getPackageByTableId(tableId);
//
//        return importList.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
//    }
//
//    @Override
//    public Set<String> getList() {
//        return baseMapper.list();
//    }
//
//    @Override
//    public boolean save(FieldTypeEntity entity) {
//        entity.setCreateTime(new Date());
//        return super.save(entity);
//    }
}